<template>
    <div class="register">
      <el-card class="card">
        <h3 class="card-title">用户注册</h3>
        <el-form 
          :model="registerForm" 
          :rules="rules" 
          ref="registerFormRef"
          @submit.prevent="handleRegister"
          class="form-container"
        >
          <el-form-item prop="username" class="input-group">
            <el-input
              v-model="registerForm.username"
              type="text"
              size="large"
              auto-complete="off"
              :prefix-icon="User"
              placeholder="请输入账号"
            />
          </el-form-item>
          <el-form-item prop="password" class="input-group">
            <el-input
              v-model="registerForm.password"
              type="password"
              size="large"
              auto-complete="off"
              :prefix-icon="Lock"
              placeholder="请输入密码"
              @keyup.enter="handleRegister"
            />
          </el-form-item>
          <el-form-item prop="phone" v-if="captchaEnabled" class="input-group">
            <el-input
              v-model="registerForm.phone"
              size="large"
              auto-complete="off"
              :prefix-icon="Phone"
              placeholder="请输入电话号码"
              style="width: 100%"
              @keyup.enter="handleRegister"
            />
          </el-form-item>
          <el-form-item prop="email" v-if="captchaEnabled" class="input-group">
            <el-input
              v-model="registerForm.email"
              size="large"
              auto-complete="off"
              :prefix-icon="Message"
              placeholder="请输入电子邮箱"
              style="width: 100%"
              @keyup.enter="handleRegister"
            />
          </el-form-item>
          <el-form-item prop="address" v-if="captchaEnabled" class="input-group">
            <el-input
              v-model="registerForm.address"
              size="large"
              auto-complete="off"
              :prefix-icon="Location"
              placeholder="请输入地址"
              style="width: 100%"
              @keyup.enter="handleRegister"
            />
          </el-form-item>
          <el-form-item prop="sex" v-if="captchaEnabled" class="input-group">
            <el-select
              v-model="registerForm.sex"
              placeholder="请选择性别"
              style="width: 100%"
            >
              <el-option label="男" value="male"></el-option>
              <el-option label="女" value="female"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item style="width:100%;">
            <el-button
              :loading="loading"
              size="large"
              type="primary"
              class="inputButton"
              @click="handleRegister"
            >
              <span v-if="!loading">注 册</span>
              <span v-else>注 册 中...</span>
            </el-button>
          </el-form-item>
          <el-form-item>
            <div style="float: right;">
              <router-link class="intton" :to="'/login'">已有账号？立即登录</router-link>
            </div>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </template>
  
  <script setup>
  import { ref, reactive } from 'vue';
  import { ElMessage } from 'element-plus';
  import {registerUser} from '@/api/user';
  import { useRouter } from 'vue-router';
  
  // 导入图标组件
  import { User, Lock, Phone, Message, Location } from '@element-plus/icons-vue';
  
  const registerFormRef = ref(null);
  const loading = ref(false);
  const captchaEnabled = ref(true); // 根据实际需求调整
  
  // 表单数据
  const registerForm = reactive({
    username: '',
    password: '',
    phone: '',
    email: '',
    address: '',
    sex: ''
  });
  
  // 验证规则
  const rules = {
    username: [
      { required: true, message: '请输入账号', trigger: 'blur' },
      { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
    ],
    phone: [
      { required: true, message: '请输入电话号码', trigger: 'blur' },
      { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的电话号码', trigger: 'blur' }
    ],
    email: [
      { required: true, message: '请输入电子邮箱', trigger: 'blur' },
      { type: 'email', message: '请输入正确的电子邮件地址', trigger: ['blur', 'change'] }
    ],
    address: [
      { required: true, message: '请输入地址', trigger: 'blur' }
    ],
    sex: [
      { required: true, message: '请选择性别', trigger: 'change' }
    ]
  };
  
  // 路由实例
  const router = useRouter();
  
  // 注册处理函数
  async function handleRegister() {
    try {
      await registerFormRef.value.validate(); // 表单验证
      loading.value = true;
  
      const response = await registerUser( registerForm);
  
      if (response.data.success) {
        ElMessage.success('注册成功！');
        router.push({ path: '/login' }); // 注册成功后跳转到登录页面
      } else {
        ElMessage.error(response.data.message || '注册失败，请重试');
      }
    } catch (error) {
      console.error('注册失败:', error);
      ElMessage.error('注册失败，请检查输入信息或稍后再试');
    } finally {
      loading.value = false;
    }
  }
  </script>
  
  <style scoped>

  
  .register {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
     background-image: url("@/assets/images/bg_01.PNG");
     background-size: cover;
  }
  
  .card {
    position: relative;
    width: 400px;
    padding: 20px;
    background-color: white;
    border-radius: 8px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  }
  
  /* .title {
    /* text-align: center; 
    margin-bottom: 20px;
  } */
  
  .input-group {
    margin-bottom: 10px;
  }
  
  .inputButton {
    color: #71e2a3;
    background-color: #e7faf0;
    border-color: #d0f5e0;
    border: none;
    box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.2), 0 2px 2px 0 #e7faf0;
    border-radius: 8px;
    height: 40px;
    width: 80px;
    transition: all 0.3s ease;
  }
  
  .intton {
    /* color: #ff9292;
    background-color: #ffeded;
    border-color: #ffdbdb; */
    /* border: none; */
    /* box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.2), 0 2px 2px 0 #e7faf0; */
    /* border-radius: 8px; */
    /* height: 40px;
    width: 80px;
    transform: translateX(40px);
    text-decoration: none;
    display: inline-block;
    text-align: center;
    line-height: 40px; */
  }
  
  .footer_02 {
    transform: translateY(20px);
  }
  
  .link-type:hover {
    text-decoration: underline;
  }
  </style>